﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models.Product.SearchModels;
using Kaiser.Modules.Hiner.Repositories;
using Kaiser.Modules.Hiner.Services;
using System.Data;

namespace Kaiser.Modules.Hiner.Models.Product
{
    public class Product : IProductService
    {
        private readonly IProductRepositorie _productRepositorie;

        public Product(IProductRepositorie productRepositorie)
        {
            _productRepositorie = productRepositorie;
        }

        public DataGrid<ProductBase> SearchProduct(ProductSearch search, PagingInfo pageinfo)
        {
            try
            {
                return _productRepositorie.SearchProduct(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetProductList(ProductSearch search, PagingInfo pageinfo)
        {
            try
            {
                return _productRepositorie.GetProductList(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Insert(Models.Product.ProductBase model)
        {
            try
            {
                _productRepositorie.Insert(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(Models.Product.ProductBase model)
        {
            try
            {
                _productRepositorie.Update(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int Delete(object primaryId)
        {
            try
            {
                return _productRepositorie.Delete(primaryId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Models.Product.ProductBase Get(object primaryId)
        {
            try
            {
                ProductBase product = _productRepositorie.Get(primaryId);
                product.PhotoList = _productRepositorie.GetProductPhotoList(product.ProductId).ToList<Photo>();
                return product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetProductBase(string hdeCode)
        {
            try
            {
                return _productRepositorie.GetProductBase(hdeCode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<Photo> GetProductPhotoList(string objectID)
        {
            try
            {
                return _productRepositorie.GetProductPhotoList(objectID);
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region 上传


        DataGrid<Attachment> GetAttachmentList(AttachmentSearch search, PagingInfo pageinfo)
        {
            try
            {
                return _productRepositorie.GetAttachmentList(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        public void DeleteAttachment(string pid)
        {
            try
            {
                _productRepositorie.DeleteAttachment(pid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        public int InsertAttachment(Attachment model)
        {
            try
            {
                return _productRepositorie.InsertAttachment(model);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public Attachment GetAttachment(string pid)
        {
            try
            {
                return _productRepositorie.GetAttachment(pid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

     

        /// <summary>
        /// 上传查询
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <returns></returns>
        DataGrid<Attachment> IProductService.GetAttachmentList(AttachmentSearch search, PagingInfo pageinfo)
        {
            try
            {
                return _productRepositorie.GetAttachmentList(search, pageinfo);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        public bool VaidHinerCode(string code, string id)
        {
            return _productRepositorie.VaidHinerCode(code,id);
        }

        public bool ValidHDECode(string code, string id)
        {
            return _productRepositorie.ValidHDECode(code, id);
        }


        public int DeleteList(IList<string> ids)
        {
            throw new NotImplementedException();
        }
    }
}
